package com.wf.smartcomm.server.serviceImpl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Merchant;
import com.wf.smartcomm.server.dao.MerchantDao;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.MerchantService;

@Service
@Transactional
public class MerchantServiceImpl implements MerchantService {
	
	@Resource
	private SessionFactory sessionFactory;
	
	@Resource
	private MerchantDao merchantDao;

	@Override
	public void save(Merchant merchant) throws Exception {
		sessionFactory.getCurrentSession().persist(merchant);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Merchant get(String id) throws Exception {
		return (Merchant) sessionFactory.getCurrentSession().get(Merchant.class, id);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void list(Page page) throws Exception {
		Query query = sessionFactory.getCurrentSession().createQuery("select count(*) from Merchant");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = sessionFactory.getCurrentSession().createQuery("from Merchant order by createdAt desc");
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	public void update(Merchant merchant) throws Exception {
		sessionFactory.getCurrentSession().merge(merchant);
	}

	@Override
	public Page pageList(Page page,int type) throws Exception {
		return merchantDao.list(page,type);
	}

	/*@Override
	public Page getMerchantByType(Page page, int type) throws Exception {
		return merchantDao.getMerchantByType(page, type);
	}*/

}
